<?php

namespace addons\csmitsm\library\xcore\xcflow\utils;

use addons\csmitsm\library\sdnode\xstack\utils\XsJsonUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcAssertUtils;


class XcflowGraphDataUtils
{
    /**
     * @return XcflowGraphDataUtilsInstance
     */
    public static function createInstance($xcflow_id)
    {
        return new Instance($xcflow_id);
    }

    public static function appendWeighToXcflowjson($xcflowjson)
    {
        // $xcflowjsonObj = json_decode($xcflowjson, true);
        $xcflowjsonObj = XsJsonUtils::stringToJson($xcflowjson);
        $currentStatusId = $xcflowjsonObj['fstatusBeginId'];

        $fstatusWeighs = [];
        foreach ($xcflowjsonObj['fstatusList'] as $fstatus) {
            $fstatusWeighs[$fstatus['id']] = 0;
        }

        for ($i = 0, $j = count($xcflowjsonObj['flogicList']); $i < $j; $i++) {
            foreach ($xcflowjsonObj['flogicList'] as $flogic) {
                if ($flogic['flowfstatus_id'] == $currentStatusId) {
                    $pass_fstatus_id = $flogic['pass_fstatus_id'];
                    $fstatusWeighs[$pass_fstatus_id] = $fstatusWeighs[$currentStatusId] + 1;
                    $currentStatusId = $pass_fstatus_id;
                }
            }
        }

        foreach ($xcflowjsonObj['fstatusList'] as &$fstatus) {
            $fstatus['weigh'] = $fstatusWeighs[$fstatus['id']];
        }

        return XsJsonUtils::jsonToString($xcflowjsonObj);
    }
}


class Instance
{
    private $xcflow = null;
    private $xcflowjson = null;
    public function __construct($xcflow_id)
    {
        $this->xcflow = XcDaoUtils::getRowById(new \app\admin\model\csmitsm\Xcflow(), $xcflow_id);
        // $this->xcflowjson = json_decode($this->xcflow->xcflowjson, true);
        $this->xcflowjson = XsJsonUtils::stringToJson($this->xcflow->xcflowjson);
    }

    public function getXcflowjson()
    {
        return $this->xcflowjson;
    }

    // Begin # status_code <----> status_id
    public function getStatusByCode($fstatusCode)
    {
        foreach ($this->xcflowjson['fstatusList'] as $item) {
            if ($item["code"] == $fstatusCode) {
                return $item;
            }
        }
        return null;
    }
    public function getStatusById($fstatusId)
    {
        foreach ($this->xcflowjson['fstatusList'] as $item) {
            if ($item["id"] == $fstatusId) {
                return $item;
            }
        }
        return null;
    }
    // End # status_code <----> status_id


    // Begin # getStatusNode
    public function getBeginStatus()
    {
        $fstatusBeginCode = $this->getStatusById($this->xcflowjson['fstatusBeginId']);

        XcAssertUtils::assertNotNull($fstatusBeginCode, "fstatusBeginCode");
        return $fstatusBeginCode;
    }
    public function getEndStatus()
    {
        $fstatusEndCode = $this->getStatusById($this->xcflowjson['fstatusEndId']);
        XcAssertUtils::assertNotNull($fstatusEndCode, "fstatusEndCode");
        return $fstatusEndCode;
    }
    // End # getStatusNode


    // Begin # getLogic by status&logictype
    public function getLogicByStatusCode($statusCode, $logicType)
    {

        if ($statusCode == null) {
            $status = $this->getBeginStatus();
            $statusCode = $status['code'];
        }
        $status = $this->getStatusByCode($statusCode);
        return $this->getLogicByStatusId($status['id'], $logicType);
    }
    public function getLogicByStatusId($statusId, $logicType)
    {
        foreach ($this->xcflowjson["flogicList"] as $item) {
            if ($item["flowfstatus_id"] == $statusId && $item["logictype"] == $logicType) {
                return $item;
            }
        }
        return null;
    }
    public function getLogicsByStatusCode($statusCode, $logicType)
    {
        $status = $this->getStatusByCode($statusCode);
        return $this->getLogicsByStatusId($status['id'], $logicType);
    }
    public function getLogicsByStatusId($statusId, $logicType)
    {
        $items = [];
        foreach ($this->xcflowjson["flogicList"] as $item) {
            if ($item["flowfstatus_id"] == $statusId && $item["logictype"] == $logicType) {
                $items[] = $item;
            }
        }
        return $items;
    }

    public function getLogicConfigFields($logic, $configType)
    {
        $fields = [];
        if (isset($logic['config_fields'])) {
            foreach ($logic['config_fields'] as $fieldcode => $json) {
                if ($json[$configType] == "Y") {
                    $fields[] = $fieldcode;
                }
            }
        }
        return $fields;
    }

    // End # getLogic by status&logictype
}
